package cn.jxau.web.controller;

import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.jxau.bean.po.ArticleType;
import cn.jxau.bean.vo.JsonResult;
import cn.jxau.common.util.JacksonUtils;

@Controller
@RequestMapping("type")
public class ArticleTypeController extends BaseController{
	@RequestMapping("all")
	@ResponseBody
	public JsonResult getAllArticle(){
		JsonResult result = renderSuccess("响应成功");
		List<ArticleType> list = articleTypeService.getAllArticleType();
		result.addData("list", list);
		return result;
	}
	
	@RequestMapping("upname")
	@ResponseBody
	public JsonResult updateName(String articleType){
		JsonResult result = renderSuccess("响应成功");
		ArticleType type = JacksonUtils.converToBean(ArticleType.class, articleType);
		
		if(articleTypeService.updateTypeNameById(type)>0){
			result.addData("reply", 1);
		}else{
			result.addData("reply", 0);
		}
		return result;
	}
	
	@RequestMapping("delete")
	@ResponseBody
	public JsonResult deleteById(Long id,boolean isParent){
		JsonResult result = renderSuccess("响应成功");
		if(articleTypeService.deleteTypeParentById(id,isParent)>0){
			result.addData("reply", 1);
		}else{
			result.addData("reply", 0);
		}
		return result;
	}
	
	@RequestMapping("insert")
	@ResponseBody
	public JsonResult insertType(String articleType){
		JsonResult result = renderSuccess("响应成功");
		ArticleType type = JacksonUtils.converToBean(ArticleType.class, articleType);
		Long insertID = articleTypeService.insertType(type);
		if(insertID>0){
			result.addData("reply", 1);
			result.addData("insertID", insertID);
		}else{
			result.addData("reply", 0);
		}
		return result;
	}
	
	@RequestMapping("drag")
	@ResponseBody
	public JsonResult insertDragType(Long id, String articleType){
		JsonResult result = renderSuccess("响应成功");
		ArticleType type = JacksonUtils.converToBean(ArticleType.class, articleType);
		if(articleTypeService.insertDragType(id,type)>0){
			result.addData("reply", 1);
		}else{
			result.addData("reply", 0);
		}
		return result;
	}
}
